import 'dart:async';
import 'package:floor/floor.dart';
import 'package:sqflite/sqflite.dart' as sqflite;

import '../dao/user_dao.dart';
import '../entities/user_entity.dart';

part 'app_database.g.dart'; // 这个文件将由代码生成器生成

@Database(version: 1, entities: [User])
abstract class AppDatabase extends FloorDatabase {
  UserDao get userDao;

  // 数据库实例
  static AppDatabase? _instance;

  // 获取数据库实例（单例模式）
  static Future<AppDatabase> getInstance() async {
    _instance ??= await $FloorAppDatabase.databaseBuilder('app_database.db').build();
    return _instance!;
  }

  // 重置数据库实例（用于测试或重新初始化）
  static void resetInstance() {
    _instance = null;
  }

  // 关闭数据库
  static Future<void> closeDatabase() async {
    if (_instance != null) {
      await _instance!.close();
      _instance = null;
    }
  }
}